home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacGS 2.5.2ß3 / (MacGSLib.π) / gp_macui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  17.6 KB  |  1,088 lines  |  [TEXT/R*ch]

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_macui.c */
  21. /* Macintosh command user interface for Ghostscript library */
  22.  
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <Icons.h>
  27. #include <console.h>
  28. #include "gx.h"                /* for gx_bitmap; includes std.h */
  29. #include "iref.h"
  30. #include "errors.h"
  31. #include "oper.h"
  32. #include "stream.h"
  33. #include "state.h"
  34. #include "store.h"
  35. #include "main.h"
  36. #include "gxdevice.h"
  37. #include "gp_mac.h"
  38. #include "gmacres.h"
  39. #include "gp_macui.h"
  40. #include "drvr.h"
  41.  
  42.  
  43. /* ---------- Ghostscript Macintosh Command User Interface ---------- */
  44.  
  45. DrvrRec drvr =
  46.     {
  47.         0x0760, 0, 0x016A, GUIMenuID,
  48.         OFS (drvrRTS), OFS (drvrRTS), OFS (vCtl), OFS (drvrRTS), OFS (vClose),
  49.         "\p.gsmacui",
  50.         0x4E75,                                        /*    RTS        */
  51.         {0x4EF9 /* JMP */, (int (*) ()) NULL},        /*    vCtl    */
  52.         {0x4EF9 /* JMP */, (int (*) ()) NULL}        /*    vClose    */
  53.     };
  54. DrvrH    drvrH = (DrvrH) NULL;
  55.  
  56.  
  57. static Handle        guiMenuBar        = (Handle) NULL;
  58. static MenuHandle    hGUIMenu        = (MenuHandle) NULL;
  59. static short        numGUIMenuItems = 0;
  60. static MenuHandle    hOrigGUIMenu    = (MenuHandle) NULL;
  61. static GUIProcPtr    pGUIProcs        = (GUIProcPtr) NULL;
  62. static short        old_mac_pause_atexit;
  63. static char            macuiMenuKeys[32];
  64. static char            drvrMenuKeys[32];
  65.  
  66.  
  67. #define CALLBACK(procName, args)                            \
  68. {                                                            \
  69.     if (pGUIProcs /* != (GUIProcPtr) NULL */ &&                \
  70.         pGUIProcs->procName /* != (void) (*) (...) NULL */)    \
  71.     {                                                        \
  72.         (*pGUIProcs->procName) args;                        \
  73.     }                                                        \
  74. }
  75.  
  76.  
  77. #define MENUAPPENDSTR(index, menuH)                            \
  78. {                                                            \
  79.     GetIndString (string, MACMENUSTRS_RES_ID, index);        \
  80.     AppendMenu (menuH, string);                                \
  81. }
  82.  
  83.  
  84. void    gp_macui_open (void);
  85.  
  86.  
  87.     void
  88. gp_macui_open (void)
  89.  
  90. {
  91.     (void) macOpenGUIDriver ((char *) NULL);
  92. }
  93.  
  94.  
  95.     void
  96. gp_macui_console_puts (void)
  97.  
  98. {
  99.     CALLBACK (DoConsolePuts, ());
  100. }
  101.  
  102.  
  103. /*
  104.  *  macOpenGUIDriver -- installs and initializes the graphical user interface
  105.  *                        driver (which spoofs being a DA).
  106.  *
  107.  */
  108.  
  109.     short
  110. macOpenGUIDriver (const char *devName)
  111.  
  112. {
  113.     static short    gsMacUIRefNum = 0;
  114.     GUIProcPtr        newGUIProcs;
  115.     Boolean            fDoInit          = FALSE;
  116.     void            GetUIMenuKeys (MenuHandle hMenu, char *pKeys, short offset);
  117.     void            AppendUIMenuKey (char cmdChar, char *pKeys);
  118.     void            patchConsoleControl (void);
  119.  
  120.  
  121.     if ((newGUIProcs = FindGUIProcs (devName)) != pGUIProcs)
  122.     {
  123.         if (pGUIProcs /* != (GUIProcPtr) NULL */)
  124.         {
  125.             CALLBACK (DoTerminate, ());
  126.  
  127.             /*...Restore the MacGS menu to its original state...*/
  128.  
  129.             if (hOrigGUIMenu /* != (MenuHandle) NULL */ &&
  130.                 numGUIMenuItems /* > 0 */ &&
  131.                 numGUIMenuItems < CountMItems (hGUIMenu))
  132.             {
  133.                 DeleteMenu ((*hGUIMenu)->menuID);
  134.                 DisposeMenu (hGUIMenu);
  135.                 hGUIMenu = hOrigGUIMenu;
  136.                 (void) HandToHand ((Handle *) &hGUIMenu);
  137.                 InsertMenu (hGUIMenu, (*hGUIMenu)->menuID);
  138.             }
  139.         }
  140.  
  141.         pGUIProcs = newGUIProcs;
  142.         fDoInit      = TRUE;
  143.     }
  144.  
  145.     /*...If necessary, install the MacGS GUI driver...*/
  146.  
  147.     if (gsMacUIRefNum == 0)
  148.     {
  149.         DCtlHandle            dceH;
  150.         register DCtlPtr    dce;
  151.  
  152.  
  153.         /*...Create driver on heap...*/
  154.  
  155.         if (!drvrH)
  156.         {
  157.             Str255        string;
  158.             int            doClose (void);
  159.             int            doPBControl (void);
  160.  
  161.  
  162.             /*...Create a new menu...*/
  163.  
  164.             GetIndString (string, MACSTRS_RES_ID, iMacGSMenuNameStr);
  165.             InsertMenu (hGUIMenu = NewMenu (GUIMenuID, string), 0);
  166.  
  167.             MENUAPPENDSTR (iAboutMenuStr    , hGUIMenu);
  168.             MENUAPPENDSTR (iOpenFileMenuStr    , hGUIMenu);
  169.             MENUAPPENDSTR (iConfigureMenuStr, hGUIMenu);
  170.             MENUAPPENDSTR (iLaserPrepMenuStr, hGUIMenu);
  171.  
  172.             numGUIMenuItems = CountMItems (hGUIMenu);
  173.  
  174.             GetUIMenuKeys (hGUIMenu, macuiMenuKeys, 0);
  175.             AppendUIMenuKey ('Q', macuiMenuKeys);        /*    preempt Cmd-Q    */
  176.  
  177.             hOrigGUIMenu = hGUIMenu;
  178.             (void) HandToHand ((Handle *) &hOrigGUIMenu);
  179.  
  180.             guiMenuBar = GetMenuBar ();
  181.  
  182.             old_mac_pause_atexit = console_options.pause_atexit;
  183.  
  184.             DrawMenuBar ();
  185.  
  186.             /*...Install the driver...*/
  187.  
  188.             drvr.vCtl.vCode      = doPBControl;
  189.             drvr.vClose.vCode = doClose;
  190.  
  191.             asm
  192.             {
  193.                 lea        drvr,a0
  194.                 move.l    #sizeof(drvr),d0
  195.                 _PtrToHand
  196.                 move.l    a0,drvrH
  197.             }
  198.  
  199.             patchConsoleControl ();
  200.         }
  201.  
  202.         /*...Find an available slot in the unit table...*/
  203.  
  204.         for (gsMacUIRefNum = 27; dceH = UTableBase[gsMacUIRefNum]; gsMacUIRefNum++)
  205.         {
  206.             if (!((*dceH)->dCtlFlags & dOpened))
  207.                 break;
  208.         }
  209.         gsMacUIRefNum = ~gsMacUIRefNum;
  210.  
  211.         /*...Create DCE...*/
  212.  
  213.         asm
  214.         {
  215.             move.w    gsMacUIRefNum,d0
  216.             dc.w    0xA13D            ;  _DrvrInstall
  217.             movea.l    (a0),dce
  218.         }
  219.  
  220.         dce->dCtlDriver = (Ptr) drvrH;
  221.         dce->dCtlFlags    = drvr.drvrFlags;
  222.         dce->dCtlEMask    = drvr.drvrEMask;
  223.         dce->dCtlMenu    = drvr.drvrMenu;
  224.     }
  225.  
  226.     if (fDoInit)
  227.     {
  228.         CALLBACK (DoInit, (hGUIMenu, numGUIMenuItems));
  229.         GetUIMenuKeys (hGUIMenu, drvrMenuKeys, numGUIMenuItems);
  230.  
  231.         DrawMenuBar ();
  232.     }
  233.  
  234.     return gsMacUIRefNum;
  235. }
  236.  
  237.  
  238. /*
  239.  *  doClose -- handles _PBClose call
  240.  *
  241.  *  Normally, we shouldn't get a close call.  If we do reach here,
  242.  *    we can keep from crashing (except on 64K ROMs) by refusing to close.
  243.  *
  244.  */
  245.  
  246.     static int
  247. doClose (void)
  248.  
  249. {
  250.     return closErr;
  251. }
  252.  
  253.  
  254. /*
  255.  *  doPBControl -- handle _PBControl call
  256.  *
  257.  *    Note that this driver spoofs being a desk accessory because the "real" event
  258.  *    loop is in <console.c>, which, for obvious reasons, we don't want to modify.
  259.  *
  260.  */
  261.  
  262.     static int
  263. doPBControl (void)
  264.  
  265. {
  266.     register CntrlParam       *pb;
  267.     DCtlPtr                    dce;
  268.     long                    oldA5 = SetCurrentA5 ();
  269.  
  270.  
  271.     asm
  272.     {
  273.         move.l    a0,pb
  274.         move.l    a1,dce
  275.     }
  276.  
  277.     switch (pb->csCode)
  278.     {
  279.         case accMenu:
  280.         {
  281.             void    doMenu (short menuID, short menuItem);
  282.  
  283.  
  284.             doMenu (pb->csParam[0], pb->csParam[1]);
  285.  
  286.             break;
  287.         }
  288.  
  289.         case accCursor:
  290.         {
  291.             CALLBACK (DoCursor, ());
  292.  
  293.             break;
  294.         }
  295.  
  296.         case accCut:
  297.         {
  298.             CALLBACK (DoCut, ());
  299.  
  300.             break;
  301.         }
  302.  
  303.         case accCopy:
  304.         {
  305.             CALLBACK (DoCopy, ());
  306.  
  307.             break;
  308.         }
  309.  
  310.         case accPaste:
  311.         {
  312.             CALLBACK (DoPaste, ());
  313.  
  314.             break;
  315.         }
  316.  
  317.         case accClear:
  318.         {
  319.             CALLBACK (DoClear, ());
  320.  
  321.             break;
  322.         }
  323.  
  324.         case accEvent:
  325.         {
  326.             register EventRecord   *pEvent = *(EventRecord **) pb->csParam;
  327.             void                    doEvent (EventRecord *pEvent);
  328.  
  329.  
  330.             doEvent (pEvent);
  331.  
  332.             break;
  333.         }
  334.  
  335.         default:
  336.         {
  337. #if 0
  338.             short    csCode = pb->csCode;
  339.  
  340.  
  341.             DebugStr ("\pdoPBControl bad csCode");
  342.             asm
  343.             {
  344.                 clr.l    d0
  345.                 move.w    csCode,d0
  346.             }
  347. #endif
  348.  
  349.             break;
  350.         }
  351.     }
  352.  
  353.     HUnlock ((Handle) drvrH);
  354.     HUnlock (RecoverHandleSys ((Ptr) dce));
  355.     SetA5 (oldA5);
  356.  
  357.     return 0;
  358. }
  359.  
  360.  
  361. enum
  362. {
  363.     iOKOutline = OK + 1,
  364.     iIconPicture,
  365.     iVersionItem
  366. };
  367.  
  368.  
  369. /*
  370.  *    doMenu -- handles menu events
  371.  *
  372.  */
  373.  
  374.     static void
  375. doMenu (short menuID, short menuItem)
  376.  
  377. {
  378.     switch (menuID)
  379.     {
  380.         case AppleMenuID:
  381.         {
  382.             MenuHandle    appleMenu = GetMHandle (AppleMenuID);
  383.             Str255        buf;
  384.  
  385.  
  386.             GetItem (appleMenu, menuItem, buf);
  387.             OpenDeskAcc (buf);
  388.  
  389.             break;
  390.         }
  391.  
  392.         case FileMenuID:
  393.         {
  394.             extern short old_mac_pause_atexit;
  395.             extern void     gs_exit (int code);
  396.  
  397.  
  398.             console_options.pause_atexit = old_mac_pause_atexit;
  399.  
  400.             gs_exit (0);
  401.  
  402.             /* unreachable */
  403.         }
  404.  
  405.         case EditMenuID:
  406.         {
  407.             switch (menuItem)
  408.             {
  409.                 case iCut:
  410.                     CALLBACK (DoCut, ());
  411.                     break;
  412.  
  413.                 case iCopy:
  414.                     CALLBACK (DoCopy, ());
  415.                     break;
  416.  
  417.                 case iPaste:
  418.                     CALLBACK (DoPaste, ());
  419.                     break;
  420.  
  421.                 case iClear:
  422.                     CALLBACK (DoClear, ());
  423.                     break;
  424.             }
  425.  
  426.             break;
  427.         }
  428.  
  429.         default:
  430.         {
  431.             switch (menuItem)
  432.             {
  433.                 case iAboutMacGS:
  434.                 {
  435.                     DialogPtr dialog = GetNewDialog (MACSPLASHDLOG, (char *) NULL,
  436.                                                      (WindowPtr) -1);
  437.  
  438.  
  439.                     if (dialog /* != (DialogPtr) NULL */)
  440.                     {
  441.                         short    item;
  442.                         short    iType;
  443.                         Handle    hItem;
  444.                         Rect    rect;
  445.  
  446.  
  447.                         /*...Prep the version field...*/
  448.  
  449.                         GetDItem (dialog, iVersionItem, &iType, &hItem, &rect);
  450.  
  451.                         if (hItem /* != (Handle) NULL */ &&
  452.                             iType == statText | itemDisable)
  453.                         {
  454.                             Handle    hFSF = GetResource (rFSFType, rFSFID);
  455.  
  456.  
  457.                             if (hFSF /* != (Handle) NULL */)
  458.                             {
  459.                                 HLock (hFSF);
  460.                                 SetIText (hItem, (StringPtr) *hFSF);
  461.                                 ReleaseResource (hFSF);
  462.                             }
  463.                         }
  464.  
  465.                         /*...Prep outline field...*/
  466.  
  467.                         GetDItem (dialog, iOKOutline, &iType, &hItem, &rect);
  468.  
  469.                         if (iType == userItem | itemDisable)
  470.                             SetDItem (dialog, iOKOutline, iType, (Handle) drawOutline,
  471.                                       &rect);
  472.  
  473.                         ShowWindow (dialog);
  474.                         ModalDialog ((ProcPtr) NULL, &item);
  475.                         DisposDialog (dialog);
  476.                     }
  477.  
  478.                     break;
  479.                 }
  480.  
  481.                 case iOpenFile:
  482.                 {
  483.                     void    doOpenFile (void);
  484.  
  485.  
  486.                     doOpenFile ();
  487.  
  488.                     break;
  489.                 }
  490.  
  491.                 case iConfigure:
  492.                 {
  493.                     void    DoSetDeviceDialog (void);
  494.  
  495.  
  496.                     DoSetDeviceDialog ();
  497.  
  498.                     break;
  499.                 }
  500.  
  501.                 case iLaserPrep:
  502.                 {
  503.                     void    doLoadLaserPrep (void);
  504.  
  505.  
  506.                     doLoadLaserPrep ();
  507.  
  508.                     break;
  509.                 }
  510.  
  511.                 default:
  512.                 {
  513.                     CALLBACK (DoMenu, (menuItem));
  514.                     break;
  515.                 }
  516.             }
  517.         }
  518.     }
  519.  
  520.     HiliteMenu (0);
  521. }
  522.  
  523.  
  524. /*
  525.  *    doEvent -- handles events
  526.  *
  527.  */
  528.  
  529.     static void
  530. doEvent (EventRecord *pEvent)
  531.  
  532. {
  533.     switch (pEvent->what)
  534.     {
  535.         case activateEvt:
  536.         {
  537.             CWindowPtr    windowPtr = (CWindowPtr) pEvent->message;
  538.             Boolean        fDoActive = pEvent->modifiers & activeFlag;
  539.  
  540.  
  541.             CALLBACK (DoActivate, (fDoActive, windowPtr));
  542.  
  543.             break;
  544.         }
  545.  
  546.         case updateEvt:
  547.         {
  548.             CWindowPeek    pWindowPeek = (CWindowPeek) pEvent->message;
  549.  
  550.  
  551.             if (!EmptyRgn (pWindowPeek->updateRgn))
  552.             {
  553.                 SetCursor (*GetCursor (watchCursor));
  554.  
  555.                 SetPort ((GrafPtr) pWindowPeek);
  556.  
  557.                 BeginUpdate ((WindowPtr) pWindowPeek);
  558.                     CALLBACK (DoUpdate, ((CWindowPtr) pWindowPeek));
  559.                 EndUpdate ((WindowPtr) pWindowPeek);
  560.  
  561.                 asm
  562.                 {
  563.                     movea.l    (a5),a0
  564.                     pea        -108(a0)            ;  arrow
  565.                     _SetCursor
  566.                 }
  567.             }
  568.  
  569.             break;
  570.         }
  571.  
  572.         case mouseDown:
  573.         {
  574.             CALLBACK (DoMouseDown, (pEvent));
  575.  
  576.             break;
  577.         }
  578.  
  579.         case autoKey:
  580.         case keyDown:
  581.         {
  582.             void    doKeyEvent (EventRecord *pEvent);
  583.  
  584.  
  585.             doKeyEvent (pEvent);
  586.  
  587.             break;
  588.         }
  589.     }
  590. }
  591.  
  592.  
  593. /*
  594.  *    doKeyEvent -- handles keyboard events
  595.  *
  596.  */
  597.  
  598. enum
  599. {
  600.     iPostDoNothing,
  601.     iPostExitToShell
  602. };
  603.  
  604.  
  605.     static void
  606. doKeyEvent (EventRecord *pEvent)
  607.  
  608. {
  609.     char    aChar = pEvent->message & charCodeMask;
  610.  
  611.  
  612.     if ('a' <= aChar && aChar <= 'z')
  613.     {
  614.         aChar += 'A' - 'a';
  615.         pEvent->message &= ~charCodeMask;
  616.         pEvent->message |= aChar;
  617.     }
  618.  
  619.     CALLBACK (DoKeyEvent, (pEvent));
  620.  
  621.     if (pEvent->modifiers & cmdKey)
  622.     {
  623.         short    flashMenuID = 0;
  624.         char    iPostAction = iPostDoNothing;
  625.  
  626.  
  627.         switch (aChar)
  628.         {
  629.             case 'O':
  630.             {
  631.                 void    doOpenFile (void);
  632.  
  633.  
  634.                 FlashMenuBar (MacGSMenuID);
  635.                 flashMenuID = MacGSMenuID;
  636.  
  637.                 doOpenFile ();
  638.  
  639.                 break;
  640.             }
  641.  
  642.             case 'L':
  643.             {
  644.                 void    doLoadLaserPrep (void);
  645.  
  646.  
  647.                 FlashMenuBar (MacGSMenuID);
  648.                 flashMenuID = MacGSMenuID;
  649.  
  650.                 doLoadLaserPrep ();
  651.  
  652.                 break;
  653.             }
  654.  
  655.             case 'Q':
  656.             {
  657.                 console_options.pause_atexit = old_mac_pause_atexit;
  658.  
  659.                 flashMenuID    = FileMenuID;
  660.                 iPostAction = iPostExitToShell;
  661.                 break;
  662.             }
  663.  
  664.             default:
  665.             {
  666.                 break;
  667.             }
  668.         }
  669.  
  670.         if (flashMenuID /* != 0 */)
  671.         {
  672.             FlashMenuBar (flashMenuID);
  673.             if (flashMenuID > 0)
  674.                 FlashMenuBar (flashMenuID);
  675.             HiliteMenu (0);
  676.         }
  677.  
  678.         switch (iPostAction)
  679.         {
  680.             case iPostExitToShell:
  681.             {
  682.                 doMenu (FileMenuID, 1 /* console File menu only has one item */);
  683.  
  684.                 /*    unreachable    */
  685.             }
  686.  
  687.             default:
  688.                 break;
  689.         }
  690.     }
  691. }
  692.  
  693.  
  694.     pascal void
  695. drawOutline (WindowPtr dialog, short item)
  696.  
  697. {
  698.     short    iType;
  699.     Handle    hItem;
  700.     Rect    rect;
  701.     GrafPtr    savePort;
  702.  
  703.  
  704.     GetPort (&savePort);
  705.     SetPort ((GrafPtr) dialog);
  706.         GetDItem (dialog, item, &iType, &hItem, &rect);
  707.         PenSize (3, 3);
  708.         FrameRoundRect (&rect, 16, 16);
  709.     SetPort (savePort);
  710. }
  711.  
  712.  
  713.     GUIProcPtr
  714. FindGUIProcs (const char *devName)
  715.  
  716. {
  717.     GUIProcPtr            retVal = (GUIProcPtr) NULL;
  718.     gx_device          **pDevice;
  719.     extern    gx_device  *gx_device_list[];
  720.  
  721.  
  722.     if (devName == (char *) NULL)
  723.         return retVal;
  724.  
  725.     for (pDevice = gx_device_list; *pDevice /* != (gx_device *) NULL */; pDevice++)
  726.     {
  727.         if (strcmp ((*pDevice)->dname, devName) == 0)
  728.         {
  729.             gx_device_macui       *pGUIDevice = (gx_device_macui *) *pDevice;
  730.  
  731.  
  732.             if (pGUIDevice->magicNumber == GUI_MAGIC_NUMBER)
  733.                 retVal = pGUIDevice->pGUIProcs;
  734.  
  735.             break;
  736.         }
  737.     }
  738.  
  739.     return retVal;
  740. }
  741.  
  742.  
  743.     static void
  744. GetUIMenuKeys (MenuHandle hMenu, char *pKeys, short offset)
  745.  
  746. {
  747.     short    numMenuItems = CountMItems (hMenu);
  748.     short    count         = 0;
  749.     short    i;
  750.  
  751.  
  752.     for (i = offset + 1; i <= numMenuItems; i++)
  753.     {
  754.         short    cmdChar;
  755.  
  756.  
  757.         GetItemCmd (hMenu, i, &cmdChar);
  758.         if (cmdChar != hMenuCmd && cmdChar /* != 0 */)
  759.         {
  760.             count++;
  761.             *(pKeys + count) = cmdChar;
  762.         }
  763.     }
  764.  
  765.     *pKeys = count;
  766. }
  767.  
  768.  
  769.     static void
  770. AppendUIMenuKey (char cmdChar, char *pKeys)
  771.  
  772. {
  773.     short    count = *pKeys;
  774.  
  775.  
  776.     count++;
  777.     *(pKeys + count) = cmdChar;
  778.     *pKeys = count;
  779. }
  780.  
  781.  
  782.     static Boolean
  783. IsUIMenuKey (char aChar, char *pKeys)
  784.  
  785. {
  786.     short    numKeys    = *pKeys;
  787.     short    i;
  788.     Boolean    retVal    = FALSE;
  789.  
  790.  
  791.     for (i = 1; i <= numKeys; i++)
  792.     {
  793.         if (*(pKeys + i) == aChar)
  794.         {
  795.             retVal = TRUE;
  796.             break;
  797.         }
  798.     }
  799.  
  800.     return retVal;
  801. }
  802.  
  803.  
  804. static int    (*pConsolePBControl) (void);
  805. static int    uiConsolePBControl (void);
  806.  
  807.  
  808.     static void
  809. patchConsoleControl (void)
  810.  
  811. {
  812.     extern WindowPtr    gConsoleWinPtr;        //    console window pointer
  813.     short                consoleRefNum = ~((WindowPeek) gConsoleWinPtr)->windowKind;
  814.     DCtlHandle            dceH           = UTableBase[consoleRefNum];
  815.     DrvrH                hConsoleDrvr  = (DrvrH) (*dceH)->dCtlDriver;
  816.  
  817.  
  818.     pConsolePBControl            = (*hConsoleDrvr)->vCtl.vCode;
  819.     (*hConsoleDrvr)->vCtl.vCode = uiConsolePBControl;
  820. }
  821.  
  822.  
  823.     static int
  824. uiConsolePBControl (void)
  825.  
  826. {
  827.     register CntrlParam       *pb;
  828.     DCtlPtr                    dce;
  829.     long                    oldA5 = SetCurrentA5 ();
  830.     int                        (*pPBControl) (void);
  831.     int                        status;
  832.  
  833.  
  834.     asm
  835.     {
  836.         move.l    a0,pb
  837.         move.l    a1,dce
  838.     }
  839.  
  840.     pPBControl = pConsolePBControl;
  841.  
  842.     if  (pb->csCode == accEvent)
  843.     {
  844.         register EventRecord   *pEvent = *(EventRecord **) pb->csParam;
  845.  
  846.  
  847.         switch (pEvent->what)
  848.         {
  849.             case autoKey:
  850.             case keyDown:
  851.             {
  852.                 if (pEvent->modifiers & cmdKey)
  853.                 {
  854.                     char    aChar = pEvent->message & charCodeMask;
  855.  
  856.  
  857.                     if ('a' <= aChar && aChar <= 'z')
  858.                     {
  859.                         aChar += 'A' - 'a';
  860.                         pEvent->message &= ~charCodeMask;
  861.                         pEvent->message |= aChar;
  862.                     }
  863.  
  864.                     if (IsUIMenuKey (aChar, macuiMenuKeys) ||
  865.                         IsUIMenuKey (aChar,    drvrMenuKeys))
  866.                     {
  867.                         pPBControl = doPBControl;
  868.                     }
  869.                 }
  870.  
  871.                 break;
  872.             }
  873.  
  874.             default:
  875.                 break;
  876.         }
  877.     }
  878.  
  879.     asm
  880.     {
  881.         move.l    pb,a0
  882.         move.l    dce,a1
  883.         move.l    pPBControl,a2        ;    status = (*pPBControl) ();
  884.         jsr        (a2)
  885.         move.w    d0,status
  886.     }
  887.  
  888.     SetA5 (oldA5);
  889.  
  890.     return status;
  891. }
  892.  
  893.  
  894. static Boolean        fDoMacOpenFile     = FALSE;
  895. short                doMacOpenFile (ref *pRunFile);
  896.  
  897.  
  898.     void
  899. SetDoMacOpenFile (Boolean fFlag)
  900.  
  901. {
  902.     fDoMacOpenFile = fFlag;
  903. }
  904.  
  905.  
  906.     Boolean
  907. GetDoMacOpenFile (void)
  908.  
  909. {
  910.     return fDoMacOpenFile;
  911. }
  912.  
  913.  
  914. #define DEFAULT_BUFFER_SIZE        512
  915.  
  916.  
  917. #define CSTR_FROM_RES(index, pCstr)                    \
  918. {                                                    \
  919.     Str255    string;                                    \
  920.                                                     \
  921.                                                     \
  922.     GetIndString (string, MACSTRS_RES_ID, index);    \
  923.     if (string[0] > 31)                                \
  924.         string[0] = 31;                                \
  925.     (void) CfromPStr (pCstr, (char *) string);        \
  926. }
  927.  
  928.  
  929.     short
  930. doMacOpenFile (ref *pRunfile)
  931.  
  932. {
  933.     short        code = e_undefinedfilename;
  934.  
  935.  
  936.     if (fDoMacOpenFile)
  937.     {
  938.         SFTypeList    types = {'TEXT', 'EPSF', 0L, 0L};
  939.         SFReply        reply;
  940.  
  941.  
  942.         SFGetFile (gSFWhere, "\p", (ProcPtr) NULL, 2, types, (ProcPtr) NULL, &reply);
  943.  
  944.         if (reply.good)
  945.         {
  946.             short    oldVRefNum;
  947.             stream *s;
  948.  
  949.  
  950.             (void) CfromPStr (gSFFName, (char *) reply.fName);
  951.             GetVol ((StringPtr) NULL, &oldVRefNum);
  952.             SetVol ((StringPtr) NULL, reply.vRefNum);
  953.                 code = file_open (gSFFName, (uint) strlen (gSFFName), "r",
  954.                                   DEFAULT_BUFFER_SIZE, pRunfile, &s);
  955.             SetVol ((StringPtr) NULL, oldVRefNum);
  956.  
  957.             /*...Create a save picture name template...*/
  958.  
  959.             {
  960.                 short    lenStr;
  961.                 short    lenExt;
  962.                 char    psExtension[32];
  963.                 char    pictExtension[32];
  964.                 char   *p;
  965.  
  966.  
  967.                 CSTR_FROM_RES (iPSExtensionStr    , psExtension  );
  968.                 CSTR_FROM_RES (iPictExtensionStr, pictExtension);
  969.  
  970.                 if ((p = strstr (gSFFName, psExtension)) /* != (char *) NULL */)
  971.                     *p = '\0';
  972.  
  973.                 lenStr = strlen (gSFFName);
  974.                 lenExt = strlen (pictExtension);
  975.  
  976.                 if (lenStr + lenExt > 31)
  977.                 {
  978.                     if (31 - lenExt >= 0)
  979.                     {
  980.                         lenStr = 31 - lenExt;
  981.                         gSFFName[lenStr] = '\0';
  982.                     }
  983.                     else
  984.                     {
  985.                         lenExt = 31 - lenStr;
  986.                         pictExtension[lenExt] = '\0';
  987.                     }
  988.                 }
  989.  
  990.                 strcat (gSFFName, pictExtension);
  991.             }
  992.         }
  993.     }
  994.  
  995.     return code;
  996. }
  997.  
  998.  
  999. /*
  1000.  *    doOpenFile -- opens an input PS file for interpretation
  1001.  *
  1002.  */
  1003.  
  1004.     static void
  1005. doOpenFile (void)
  1006.  
  1007. {
  1008.     CALLBACK (DoOpenFilePre, ());
  1009.     console_options.pause_atexit = 1;
  1010.  
  1011.     (void) doRunIndString (iMacRunFile);
  1012.  
  1013.     console_options.pause_atexit = old_mac_pause_atexit;
  1014.     CALLBACK (DoOpenFilePost, ());
  1015. }
  1016.  
  1017.  
  1018.     static void
  1019. doLoadLaserPrep (void)
  1020.  
  1021. {
  1022.     SetCursor (*GetCursor (watchCursor));
  1023.  
  1024.     (void) doRunIndString (iLaserPrepStr);
  1025. }
  1026.  
  1027.  
  1028.     short
  1029. doRunIndString (short index)
  1030.  
  1031. {
  1032.     Str255    string;
  1033.     short    code;
  1034.  
  1035.  
  1036.     GetIndString (string, MACPSSTRS_RES_ID, index);
  1037.     if ((code = ResError ()) /* != noErr */)
  1038.         return code;
  1039.  
  1040.     return doRunString (PtoCstr (string));
  1041. }
  1042.  
  1043.  
  1044.     short
  1045. doRunString (char *pStr)
  1046.  
  1047. {
  1048.     short    user_errors = 1;
  1049.     short    code;
  1050.     int        exit_code;
  1051.     ref        error_object;
  1052.  
  1053.  
  1054.     console_options.pause_atexit = 1;
  1055.  
  1056.     if ((code = gs_run_string (pStr, user_errors, &exit_code, &error_object)) < 0)
  1057.     {
  1058.         extern WindowPtr    gConsoleWinPtr;        //    console window pointer
  1059.  
  1060.  
  1061.         SelectWindow (gConsoleWinPtr);
  1062.  
  1063.         zflush ((ref *) NULL);    /* flush stdout */
  1064.  
  1065.         gs_debug_dump_stack (exit_code, &error_object);
  1066.     }
  1067.  
  1068.     console_options.pause_atexit = old_mac_pause_atexit;
  1069.  
  1070.     return code;
  1071. }
  1072.  
  1073.  
  1074.     char *
  1075. CfromPStr (char *pCstr, char *pPstr)
  1076.  
  1077. {
  1078.     short        lenstr = pPstr[0];
  1079.  
  1080.  
  1081.     if (lenstr /* > 0 */)
  1082.         BlockMove (&(pPstr[1]), pCstr, lenstr);
  1083.  
  1084.     pCstr[lenstr] = '\0';
  1085.  
  1086.     return pCstr;
  1087. }
  1088.